package com.ywgt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.pojo.Goods;
import com.ywgt.pojo.GrouponRules;
import com.ywgt.mapper.GrouponRulesMapper;
import com.ywgt.service.GoodsService;
import com.ywgt.service.GrouponRulesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-03-13
 */
@Service
public class GrouponRulesServiceImpl extends ServiceImpl<GrouponRulesMapper, GrouponRules> implements GrouponRulesService {

    @Autowired
    GoodsService goodsService;
    @Override
    public Page<GrouponRules> getGrouponRules(Integer rulessId, int page, int limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        return baseMapper.selectGrouponRules(rulessId,sort,order);
    }

    @Override
    public List<Map<String,Object>> getGroupRulesAndGoods(int i, int limit) {
        List<Map<String,Object>> data=new ArrayList<>();
        Page<GrouponRules> grouponRules = this.getGrouponRules(null, i, limit, "add_time", "desc");
        for (GrouponRules grouponRule : grouponRules) {
            Goods goods = goodsService.getById(grouponRule.getGoodsId());
            if(goods==null){
                continue;
            }
            Map<String,Object> item=new HashMap<>();
            item.put("goods", goods);
            item.put("groupon_price", goods.getRetailPrice().subtract(grouponRule.getDiscount()));
            item.put("groupon_member", grouponRule.getDiscountMember());
            data.add(item);
        }
        return data;
    }

    @Override
    public List getByGoodsId(Integer id) {
        LambdaQueryWrapper<GrouponRules> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(GrouponRules::getGoodsId,id);
        return baseMapper.selectList(wrapper);
    }
}
